﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.ViewModels;
using Enterprise.Management.Business;
using Enterprise.Management.Caches;
using Enterprise.Management.Data;
using Enterprise.Management.FilterAttributes;
using Enterprise.Management.Gridview;
using Enterprise.Management.DataTemplates;

namespace Enterprise.Management.Controllers
{
    [CategoryFunction]
    public class AuditController : BaseController
    {
        AuditBLL bllAudit = new AuditBLL();

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(GridviewPage page)
        {
            var gridview = new Gridview.Gridview(page);
            gridview.Header.Add("姓名", "User_Name");
            gridview.Header.Last.SetProperty("style", "width:100px");
            gridview.Header.Add("电子邮箱", "User_Email");
            gridview.Header.Add("角色", "DP_Role.Role_Name");
            gridview.Header.Add("网点", "DP_Category.Category_Name");
            gridview.Header.Add(string.Empty);
            gridview.Header.Last.SetProperty("style", "width:80px");
            gridview.Header.Last.RowDataTemplate = new EditDeleteDataTemplate("_EditDeleteDataTemplatePartial")
            {
                Name = "User_ID",
                Edit = Url.Action("User", "Edit") + "?uid={0}",
                Delete = "delUser({0})"
            };

            DeptPortalsDataContext context = new DeptPortalsDataContext();
            var query = from u in context.DP_User
                        select u;
            var data = IntelligentQueryBuilder.Create<DP_User>(query, gridview.Page);

            gridview.Fill(data);

            return PartialView("_GridviewPartial", gridview);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(EditAuditViewModel vmAudit)
        {
            if(ModelState.IsValid)
            {
                DP_Audit_Info dp_audit_info = new DP_Audit_Info();
                dp_audit_info.AdtInf_CtgrID = BranchCategoryCache.BranchCategoryId.Value;
                dp_audit_info.AdtInf_Date = vmAudit.AdtInf_Date;
                dp_audit_info.AdtInf_DeptID = vmAudit.AdtInf_DeptID;
                dp_audit_info.AdtInf_PersonInCharge = vmAudit.AdtInf_PersonInCharge;
                dp_audit_info.AdtInf_Content = vmAudit.AdtInf_Content;
                dp_audit_info.AdtInf_Result = vmAudit.AdtInf_Result;
                dp_audit_info.AdtInf_Response = vmAudit.AdtInf_Response;
                dp_audit_info.AdtInf_Status = (int)StatusType.Valid;
                dp_audit_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_audit_info.ModifiedDate = DateTime.Now;

                if (bllAudit.Add(dp_audit_info))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "新建失败，请稍后再试！");
                }
            }

            return View(vmAudit);
        }

        public ActionResult Edit(int id)
        {
            DP_Audit_Info dp_audit_info = bllAudit.Get(id);
            EditAuditViewModel vmAudit = new EditAuditViewModel();
            vmAudit.Id = dp_audit_info.AdtInf_ID;
            vmAudit.AdtInf_Date = dp_audit_info.AdtInf_Date;
            vmAudit.AdtInf_DeptID = dp_audit_info.AdtInf_DeptID;
            vmAudit.AdtInf_PersonInCharge = dp_audit_info.AdtInf_PersonInCharge;
            vmAudit.AdtInf_Content = dp_audit_info.AdtInf_Content;
            vmAudit.AdtInf_Result = dp_audit_info.AdtInf_Result;
            vmAudit.AdtInf_Response = dp_audit_info.AdtInf_Response;
            return View(vmAudit);
        }

        [HttpPost]
        public ActionResult Edit(EditAuditViewModel vmAudit)
        {
            if (ModelState.IsValid)
            {
                DP_Audit_Info dp_audit_info = bllAudit.Get(vmAudit.Id.Value);
                dp_audit_info.AdtInf_Date = vmAudit.AdtInf_Date;
                dp_audit_info.AdtInf_DeptID = vmAudit.AdtInf_DeptID;
                dp_audit_info.AdtInf_PersonInCharge = vmAudit.AdtInf_PersonInCharge;
                dp_audit_info.AdtInf_Content = vmAudit.AdtInf_Content;
                dp_audit_info.AdtInf_Result = vmAudit.AdtInf_Result;
                dp_audit_info.AdtInf_Response = vmAudit.AdtInf_Response;
                dp_audit_info.ModifiedBy = HtmlCache.GetUser().User_ID;
                dp_audit_info.ModifiedDate = DateTime.Now;

                if (bllAudit.Update(dp_audit_info))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError("", "更新失败，请稍后再试！");
                }
            }
            return View(vmAudit);
        }
    }
}
